<template>
  <basic-container>
    <div class="avue-crud">

      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="设备名" prop="deviceName">
          <el-input v-model="queryParams.clientName" placeholder="请输入设备名" clearable
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作工具栏 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-edit"
                     class="filter-item"
                     size="mini" @click="handleAdd"
                     v-if="['system:oauth2-client:create']">新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
      </el-row>

      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading">
        <el-table-column
          prop="id"
          header-align="center"
          align="center"
          label="自增主键">
        </el-table-column>
        <el-table-column
          prop="deviceId"
          header-align="center"
          align="center"
          label="设备id">
        </el-table-column>
        <el-table-column
          prop="deviceMac"
          header-align="center"
          align="center"
          label="设备mac地址">
        </el-table-column>
        <el-table-column
          prop="deviceName"
          header-align="center"
          align="center"
          label="设备名称">
        </el-table-column>
        <el-table-column
          prop="accessStatus"
          header-align="center"
          align="center"
          label="设备接入状态,0-未接入，1-已接入">
        </el-table-column>
        <el-table-column
          prop="accessDate"
          header-align="center"
          align="center"
          label="设备接入时间">
        </el-table-column>
        <el-table-column
          prop="sort"
          header-align="center"
          align="center"
          label="排序">
        </el-table-column>
        <el-table-column
          prop="remark"
          header-align="center"
          align="center"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="createBy"
          header-align="center"
          align="center"
          label="创建者">
        </el-table-column>
        <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          label="创建时间">
        </el-table-column>
        <el-table-column
          prop="updateBy"
          header-align="center"
          align="center"
          label="更新者">
        </el-table-column>
        <el-table-column
          prop="updateTime"
          header-align="center"
          align="center"
          label="更新时间">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-edit"
                       @click="handleUpdate(scope.row)">修改
            </el-button>
            <el-button type="text" size="small" icon="el-icon-delete"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
                  @pagination="getDataList"/>
      <!-- 对话框(添加 / 修改) -->
      <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </basic-container>
</template>

<script>
import {fetchList, delObj, addObj, getObj, putObj} from '@/api/iot/business-iot-device'
import {mapGetters} from 'vuex'

export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  created() {
    this.getDataList()
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      fetchList(Object.assign({
        current: this.pageIndex,
        size: this.pageSize
      })).then(response => {
        this.dataList = response.data.data.records
        this.total = response.data.data.total
      })
      this.dataListLoading = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getDataList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加 OAuth2 客户端";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getObj(id).then(response => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改 OAuth2 客户端";
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          putObj(this.form).then(response => {
            this.$message.success("修改成功");
            this.open = false;
            this.getDataList();
          });
          return;
        }
        // 添加的提交
        addObj(this.form).then(response => {
          this.$message.success("新增成功");
          this.open = false;
          this.getDataList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除设备编号为"' + row.clientId + '"的数据项?').then(function () {
        return delObj(id);
      }).then(() => {
        this.getDataList();
        this.$message.success("删除成功");
      }).catch(() => {
      });
    }
  }
}
</script>
